using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using BL;
using BO;
namespace AnimalFarm.Livestock_manage
{
    public partial class AddLivestock : Telerik.WinControls.UI.RadForm
    {
        B_LivestockType livestocTypekManager;
        B_Livestock livestockManager;
        B_Herd herdManager;
        B_HomeField homefieldManager;
        private int farmerID;

        public int FarmerID
        {
            get { return farmerID; }
            set { farmerID = value; }
        }
        public AddLivestock()
        {
            InitializeComponent();
            livestocTypekManager = B_LivestockType.CreateInstance();
            livestockManager = B_Livestock.CreateInstance();
            herdManager = B_Herd.CreateInstance();
            homefieldManager = B_HomeField.CreateInstance();
            //bind live stock data
            bindLivestockData();
            //bind herd data
            bindHerdData();
            //bind home field data
            bindHomefieldData();
        }
        public void bindLivestockData()
        {
            DataTable dt = livestocTypekManager.GetAllLivestockType();
            cm_livestockType.DataSource = dt;
            cm_livestockType.DisplayMember = "LivestockTypeName";
            cm_livestockType.ValueMember = "LivestockTypeID";
        }
        public void bindHerdData()
        {
            DataTable dt = herdManager.GetAllHerd();
            cm_herdType.DataSource = dt;
            cm_herdType.DisplayMember = "HerdName";
            cm_herdType.ValueMember = "HerdID";
            
        }
        public void bindHomefieldData()
        {
            DataTable dt = homefieldManager.GetAllHomeField();
            cm_hfield.DataSource = dt;
            cm_hfield.DisplayMember = "HomeFieldName";
            cm_hfield.ValueMember = "HomeFieldID";
        }
        private void dtpTime_ValueChanged(object sender, EventArgs e)
        {

        }

        private void drdField_SelectedIndexChanged(object sender, Telerik.WinControls.UI.Data.PositionChangedEventArgs e)
        {

        }

        private void btnAddDairyproduct_Click(object sender, EventArgs e)
        {
            try
            {
                //prepare data to create new instance of livestock
                int ls_typeID = Convert.ToInt32(cm_livestockType.SelectedItem.Value.ToString());
                int ls_herdID = Convert.ToInt32(cm_herdType.SelectedItem.Value.ToString());
                int ls_homeFieldID = Convert.ToInt32(cm_hfield.SelectedItem.Value.ToString());
                string maffID = cm_maffID.SelectedText;
                string name = txt_Name.Text;
                string breed = txtBreed.Text;
                string blood = cm_blood.SelectedText;
                string des = txtDes.Text;
                int weight = int.Parse(txt_weight.Text);
                DateTime dob = dtpTime.Value;

                //create new livestock object
                Livestock_Info livestock = new Livestock_Info(0, ls_typeID, name, breed, blood, dob, weight, des, maffID, true, farmerID, ls_homeFieldID, ls_herdID);
                //execute inserting data
               bool ok= livestockManager.CreateOneLivestock(livestock);
               if (ok)
                   MessageBox.Show("Add successfully");
               else
                   MessageBox.Show("Add fail");

            }catch(Exception ex)
            {
             MessageBox.Show(ex.Message);
            }
        }
    }
}
